<?php if ( ! defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );
/**
 * BackendPro
 *
 * A website backend system for developers for PHP 4.3.2 or newer
 *
 * @package         conlabz CMS
 * @author          Christian Schneider
 * @copyright       Copyright (c) 2009
 * @license         
 * @link            http://www.conlabz.de
 * @filesource
 */

// ---------------------------------------------------------------------------

/**
 * Form model
 *
 * Model used to retrive webites
 *
 * @package			conlabz CMS
 * @subpackage		Models
 */
class Form_model extends Base_model {
	function Form_model( ) {
		parent::Base_model( );

		$this->_TABLES = array( 'Option' => $this->config->item( 'backendpro_table_prefix' ) . 'article' );

		// Cache to store already fetched items
		$this->_CACHE = array( );

		// Object keyword
		// I wouldn't advise changing this, it could corrupt current
		// articles, the reason for needing this is to stop unserialze
		// errors spamming the log files
		$this->object_keyword = "BeP::Object::";

		log_message( 'debug', 'conlabz CMS : Form_model class loaded' );
	}
	
	/**
	 * Get Forms
	 *
	 * @access public
	 * @param mixed $where Where query string/array
	 * @param array $limit Limit array including offset and limit values
	 * @return object
	 */
	function getForms( $where = NULL, $limit = array( 'limit' => NULL, 'offset' => '' ) ) {
		$this->db->select( 'form.id, 
							form.title, 
							form.email, 
							form.description' );
		$this->db->from( "form" );
		$this->db->where( "website_id", $this->session->userdata['website_id'] );
		if( !is_null( $where ) ) {
			$this->db->where( $where );
		}
		if( !is_null( $limit['limit'] ) ) {
			$this->db->limit( $limit['limit'], ( isset( $limit['offset'] ) ? $limit['offset'] : '' ) );
		}
		$this->db->order_by('form.title desc'); 
		return $this->db->get( );
	}
	
	/**
	 * Get form fields
	 *
	 * @access public
	 * @param int $form_id Form ID
	 * @param mixed $where Where query string/array
	 * @param array $limit Limit array including offset and limit values
	 * @return object
	 */
	function getFormFields( $form_id, $where = NULL, $limit = array( 'limit' => NULL, 'offset' => '' ) ) {
		$this->db->select( 'form.label,
							form.type, 
							form.name,
							form.value,
							form.additional' );
		$this->db->from( "form" );
		$this->db->where( "website_id", $this->session->userdata['website_id'] );
		$this->db->where( "id", $form_id);
		
		if( !is_null( $where ) ) {
			$this->db->where( $where );
		}
		if( !is_null( $limit['limit'] ) ) {
			$this->db->limit( $limit['limit'], ( isset( $limit['offset'] ) ? $limit['offset'] : '' ) );
		}
		$this->db->order_by('form.id desc');
		 
		return $this->db->get( );
	}
	
	/**
	 * Get Entries for view file on the frontend
	 * 
	 * @param $blog_id
	 * @param $where
	 * @param $limit
	 * @return object
	 */
	function getFormView( $form_id ) {
		//get single form
		$this->db->select( 'form.title,
							form.action, 
							form.label, 
							form.type, 
							form.name,
							form.description'
						 );
		$this->db->from( "form" );
		$this->db->where( "form.id", $form_id );
		 
		$fields = $this->db->get( );
		//fields contains only a single entry
		$row = $fields->result_array( );
		$return['h1'] = $row[0]['title'];
		$return['description'] = $row[0]['description'];
		
		if( !empty ( $row[0]['label'] ) ){
			$label = unserialize( $row[0]['label'] );
		}
		if( !empty ( $row[0]['type'] ) ){
			$type = unserialize( $row[0]['type'] );
		}
		if( !empty ( $row[0]['name'] ) ){
			$name = unserialize( $row[0]['name'] );
		}
		if( !empty( $label ) ){
			foreach( $label as $key => $value ){
				
				$return['fields'][$key]['label'] = $value;
				$return['fields'][$key]['type'] = $type[$key];
				$return['fields'][$key]['name'] = $name[$key];
			}	
		}
		
		return $return;
	}

	/**
	 * Insert new Form
	 *
	 * @access public
	 * @param array $data Data array
	 */
	function insert( $data = array( ) ) {		
		//insert the new entry
		$this->db->set( 'website_id',  $this->session->userdata('website_id')    );
		$this->db->insert( 'form', $data );
		
		return $this->db->insert_id( );
	}

	/**
	 * Update Form
	 *
	 * @access public
	 * @param array $data Data array
	 */
	function update( $data = array( ), $form_id ) {		
		//update entry
		$this->db->update( 'form', $data, array( 'id' => $form_id ) );
	}
	
	/**
	 * Insert new Field
	 *
	 * @access public
	 * @param array $data Data array
	 */
	function update_field( $form_id, $data = array( ), $field_id = NULL ) {
		//@todo: add default value and additional information fields to database
		unset( $data['field_id'] );
		unset( $data['form_id'] );
		//set empty arrays
		$label = array( );
		$type = array( );
		$name = array( );
		$value = array( );
		$additional = array( );
		//select infos from form
		$fields = $this->getFormFields( $form_id );
		//unserialize info - infos are always in a single row
		$row = $fields->result_array( );
		if( !empty ( $row[0]['label'] ) ){
			$label = unserialize( $row[0]['label'] );
		}
		if( !empty ( $row[0]['type'] ) ){
			$type = unserialize( $row[0]['type'] );
		}
		if( !empty ( $row[0]['name'] ) ){
			$name = unserialize( $row[0]['name'] );
		}
		/*if( !empty ( $row[0]['value'] ) ){
			$value = unserialize( $row[0]['value'] );
		}
		if( !empty ( $row[0]['additional'] ) ){
			$additional = unserialize( $row[0]['additional'] );
		}*/
		//add new field to array
		$name_str = str_replace( " ", "", strtolower( $data['label'] ) );
		
		$label = array_merge( $label, (array)$data['label'] );
		$type = array_merge( $type, (array)$data['type'] );
		$name = array_merge( $name, (array)$name_str );
		//$value = array_merge( $label, (array)$data['value'] );
		//$additional = array_merge( $label, (array)$data['additional'] );
		
		//serialize infos
		$data['label'] = serialize( $label );
		$data['type'] = serialize( $type );
		$data['name'] = serialize( $name );
		//$data['value'] = serialize( $value );
		//$data['additional'] = serialize( $additional );
		
		//update table
		$this->db->update( 'form', $data, array( 'id' => $form_id,
												 'website_id' => $this->session->userdata( 'website_id' ) ) );
	}
	
	/**
	 * Update Blog Entry
	 *
	 * @access public
	 * @param array $data Data array
	 */
	function update_entries( $data = array( ), $entry_id ) {		
		$entry = array( );
		$entry['body']      = $data['body'];
		$entry['title']     = $data['title'];
		$entry['active']    = $data['active'];
		$entry['timestamp'] = $data['timestamp']; 
		
		//insert the new entry
		$this->db->update( 'blog_entry', $entry, array( 'entry_id' => $entry_id ) );
	}
	
	function delete_entry( $entry_id ) {
		//delete the blog entry
		$this->db->delete( 'blog_entry', array( 'entry_id' => $entry_id ) );
	}
	
	/**
	 * Constructs an array of the given forms to be used for a dropdown menu
	 *
	 * @access public
	 * @return array
	 */
	function buildFormDropdown( ){
		$forms = $this->getForms( );
		
		$dropdown = array( );
		foreach( $forms->result( ) as $row ) {
	    	 $dropdown[$row->id] = $row->title;
		}
		
		return $dropdown;
	}
}
/* End of file blog_model.php */
/* Location: ./modules/blog/models/blog_model.php */